import pymongo

class MongodbContent():
    def __init__(self,ip='192.168.7.160',user ='admin',password='sq',port=27017):
        self.client = pymongo.MongoClient(f'mongodb://{user}:{password}@{ip}:{port}')
        self.db  = self.client['sq-waimai']

    def inesrt(self,collextion,query,many=False):
        res_set = self.db[collextion]
        if many==False:
            res_set.insert_one(query)
        else:
            res_set.insert_many(query)


    def find(self,collection,query,many=True):
        res_set = self.db[collection]
        if many:
            res = res_set.find(query)
            return [one for one in res]
        else:
            result = res_set.find_one(query)
            return result

    def update(self,collection,myquery,newvalues,many=False):
        res_set = self.db[collection]
        if many==False:
            res_set.update_many(myquery,{'$set':newvalues})
        else:
            res_set.update_one(myquery,{'$set':newvalues})

    #删除
    def delete(self,collection,query,many=False):
        res_set = self.db[collection]
        if many==False:
            res_set.delete_one(query)
        else:
            res_set.delete_many(query)

if __name__ == '__main__':
    db = MongodbContent()
    res = db.find('activities',{"name":"新店"},many=False)
    print(res)



